<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>放大镜</title>
    <style>
        .box {
            width: 400px;
            height: 400px;
            position: relative;
            margin: 20px auto;
            border: 1px solid #ccc;
        }

        .small-box {
            width: 100%;
            height: 100%;
            position: relative;
        }

        .small-box img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .small-box .mask {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.2);
            position: absolute;
            top: 0px;
            left: 0px;
            cursor: pointer;
            display: none;
        }

        .big-box {
            width: 400px;
            height: 400px;
            border: 1px solid #ccc;
            overflow: hidden;
            /* display: none; */
            position: absolute;
            left: 110%;
            top: 0px;
        }
        .big-box img {
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>

<body>
    <div class="box" id="box">
        <div class="small-box">
            <img src="./img/case17/min.png" width="400" alt="">
            <div class="mask"></div>
        </div>
        <div class="big-box">
            <img src="./img/case17/big.webp" width="800" alt="">
        </div>
    </div>
    <script>
        // 获取元素
        let box = document.querySelector('#box');
        let smallBox = box.querySelector(".small-box")
        let mask = box.querySelector('.mask');
        let smallImg = box.querySelector('.small-box img');
        let bigBox = box.querySelector('.big-box');
        let bigImg = box.querySelector('.big-box img');

        // 功能一、遮罩和大图的显示隐藏
        box.addEventListener("mouseenter", function () {
            mask.style.display = "block";
            bigBox.style.display = "block";
        })
        box.addEventListener("mouseleave", function () {
            mask.style.display = "none";
            bigBox.style.display = "none";
        })

        // 功能二、遮罩跟随鼠标
        box.addEventListener("mousemove", function (e) {
            // 计算遮罩的中心点跟随鼠标
            let left = e.clientX - box.offsetLeft - mask.offsetWidth / 2
            let top = e.clientY - box.offsetTop - mask.offsetHeight / 2
            // 边界判断
            if(left > 0 && left < box.offsetWidth-mask.offsetWidth){
                mask.style.left = left + "px"
            }
            if(top > 0 && top < box.offsetHeight-mask.offsetHeight){
                mask.style.top = top + "px"
            }

            // 设置大图位置
            // 大图的位置就是遮罩的位置 因为大图是小图的两倍 所以移动大图位置的时候需要乘于2
            bigImg.style.left = "-"+parseInt(mask.style.left)*2+"px"
            bigImg.style.top = "-"+parseInt(mask.style.top)*2+"px"

        })


    </script>
</body>

</html>